/*
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 */
package org.apache.airavata.metcat.db;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class ModelProperties {

	private static ModelProperties props = null;

	private static final String CASSANDRA_PROPERTY_FILE = "../conf/cassandra.properties";

    public static final String CASSANDRA_CLUSTER_NAME_PROPERTY = "cassandraCluster.name";
    public static final String CASSANDRA_CLUSTER_PORT_PROPERTY = "cassandraCluster.port";
    public static final String CASSANDRA_CLUSTER_KEYSPACE_PROPERTY = "cassandraCluster.keyspace";

    private String cassandraCluster = null,clusterPort = null,keyspace = null;

	private ModelProperties() {
		this.loadMessageBrokerProperties();
	}

	public static ModelProperties getInstance() {
		if (props == null) {
			props = new ModelProperties();
		}
		return props;
	}

	private InputStream getCassandraPropertiesURL() {
		InputStream file;
        //load cassandra server properties from conf directory.
        try {
            file = new FileInputStream(CASSANDRA_PROPERTY_FILE);
        } catch (FileNotFoundException e) {
            file = null;
            System.out.println("Using default cassandra server properties");
        }
        if(file==null)
        {   // try to load default cassandra server properties from class loader.
            try {
                file =  ClassLoader.getSystemResource("conf/cassandra.properties").openStream();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return file;
	}

	private void loadMessageBrokerProperties() {
		Properties properties = new Properties();
        try {
            InputStream file = getCassandraPropertiesURL();
            properties.load(file);
            this.cassandraCluster = (String) properties.get(CASSANDRA_CLUSTER_NAME_PROPERTY);
            this.clusterPort = (String) properties.get(CASSANDRA_CLUSTER_PORT_PROPERTY);
            this.keyspace = (String) properties.get(CASSANDRA_CLUSTER_KEYSPACE_PROPERTY);
        } catch (IOException e) {
            e.printStackTrace();
        }
	}

	public String getCassandraCluster() {
		return cassandraCluster;
	} 
	
	public String getClusterPort() {
		return clusterPort;
	}
	
	public String getKeyspace() {
		return keyspace;
	}
	
	

}
